import { _decorator, Component, Node, ScrollView, Prefab, Label, sys, ToggleContainer, director, Toggle } from 'cc';
import { GameData } from "./framework/GameData";
// import { ResourceUtilClass } from "./framework/resourceUtilClass";
import { PoolManager } from "./framework/poolManager";
import { bdlMgr } from 'db://assets/Script/Manager/BundleManager_S';
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';
import { setLimitDepth } from './Board';
const { ccclass, property } = _decorator;

@ccclass('MainUI')
export class MainUI extends Component {
    @property(Node)
    Button_back: Node | null = null;
    @property(ToggleContainer)
    ToggleContainer_难度: ToggleContainer | null = null;

    onLoad() {
        GameData.getInstance().rootNode = this.node.parent;

        this.ShowOn();

        this.onClickMatch();
    }

    ShowOn() {
        // this.node.active = true;
        this.AddListener();
    }

    AddListener() {
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);

        console.log("AddListener----------");

        const toggles = this.ToggleContainer_难度.node.getComponentsInChildren(Toggle);
        for (const toggle of toggles) {
            toggle.node.on('toggle', this.onToggleChanged, this);
        }
    }

    onToggleChanged(toggle: Toggle) {
        if (toggle.isChecked) {
            console.log('当前选中的 Toggle 是：', toggle?.node.name);

            const toggles = this.ToggleContainer_难度.node.getComponentsInChildren(Toggle);

            const index = toggles.indexOf(toggle);

            let depth = index + 1;
            depth = Math.pow(2, depth);
            console.log("depth==", index, depth);
            setLimitDepth(depth);
        }
    }

    On_Button_back() {
        director.loadScene("MainScene");
        SSS_AudioManager.PlayButtonAudio();
    }

    async onClickMatch() {
        // 先销毁
        let chessboard = GameData.getInstance().rootNode.getChildByName("chessboard");
        if (chessboard) {
            chessboard.removeFromParent();
        }

        // 正常对局
        GameData.getInstance().matchId = 0;

        // 打开对局
        // let prefab = await ResourceUtilClass.loadTheRes("prefab/chessboard", Prefab) as Prefab;
        const prefab = await bdlMgr.getRes<Prefab>(`Xiangqi_Prefab`, `chessboard`, Prefab);
        let node = PoolManager.instance.getNode(prefab, GameData.getInstance().rootNode);

        // 隐藏主界面
        let mainui = GameData.getInstance().rootNode.getChildByName("mainui");
        mainui.active = false;
    }

    async onClickMatchList() {
        // 先销毁
        let matchList = GameData.getInstance().rootNode.getChildByName("matchList");
        if (matchList) {
            matchList.removeFromParent();
        }

        // 打开残局列表
        // let prefab = await ResourceUtilClass.loadTheRes("prefab/matchList", Prefab) as Prefab;
        const prefab = await bdlMgr.getRes<Prefab>(`Xiangqi_Prefab`, `matchList`, Prefab);
        let node = PoolManager.instance.getNode(prefab, GameData.getInstance().rootNode);

        // 隐藏主界面
        let mainui = GameData.getInstance().rootNode.getChildByName("mainui");
        mainui.active = false;
    }
}